package com.example.imchat.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.imchat.R;
import com.example.imchat.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;


/*
* 群详情界面的操作
* 20/5/24
* lee
* */
public class GroupDetailAdapter extends BaseAdapter {
    private Context mContext;

    public void setmIsCanModify(boolean mIsCanModify) {
        this.mIsCanModify = mIsCanModify;
    }

    private boolean mIsCanModify;   //是否允许添加删除 群成员
    private List<UserInfo> mUsers = new ArrayList<>();

    public boolean ismIsDeleteModify() {
        return mIsDeleteModify;
    }

    private boolean mIsDeleteModify;//删除模式 true：可以删除
    private OnGroupDetailListener mOnGroupDetailListener;

    public GroupDetailAdapter(Context Context,boolean isCanModify,OnGroupDetailListener onGroupDetailListener) {
        mContext = Context;
        mIsCanModify = isCanModify;
        mOnGroupDetailListener = onGroupDetailListener;
    }


    //刷新数据
    public void refresh(List<UserInfo> Users){
        if (null != Users && Users.size()>=0){
            mUsers.clear();

            //添加群成员后面的加减号
            initUsers();
            mUsers.addAll(0,Users);
        }
        notifyDataSetChanged();

    }

    private void initUsers() {
        UserInfo add = new UserInfo("add");
        UserInfo delete = new UserInfo("delete");
        mUsers.add(delete);
        mUsers.add(0,add);
    }


    @Override
    public int getCount() {
        return mUsers == null ? 0:mUsers.size();
    }

    @Override
    public Object getItem(int position) {
        return mUsers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取或创建viewholder
        ViewHolder holder = null;
        if (null == convertView ){
            holder = new ViewHolder();
            convertView = View.inflate(mContext,R.layout.item_groupdetail,null);
            holder.photo = convertView.findViewById(R.id.iv_group_detail_photo);
            holder.delete = convertView.findViewById(R.id.iv_group_detail_delete);
            holder.name = convertView.findViewById(R.id.tv_group_detail_name);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //获取当前item数据
        UserInfo userInfo = mUsers.get(position);
        //显示数据
        if (mIsCanModify){
            //群主或者开放权限
            if (position ==getCount() - 1){ //减号
                //是否进入删除模式的判断
               if (mIsDeleteModify){
                   convertView.setVisibility(View.INVISIBLE);
               }else {
                   convertView.setVisibility(View.VISIBLE);
                   holder.photo.setImageResource(R.drawable.groupdetail_reduce);
                   holder.photo.setMaxWidth(50);
                   holder.photo.setMaxHeight(50);
                   holder.delete.setVisibility(View.GONE);
                   holder.name.setVisibility(View.INVISIBLE);
               }

            }else if (position ==getCount() - 2){//加号
                if (mIsDeleteModify){
                    convertView.setVisibility(View.INVISIBLE);
                }else {
                    convertView.setVisibility(View.VISIBLE);
                    holder.photo.setImageResource(R.drawable.groupdetail_add);
                    holder.delete.setVisibility(View.GONE);
                    holder.name.setVisibility(View.INVISIBLE);
                }
            }else {
                //群成员
                convertView.setVisibility(View.VISIBLE);
                holder.name.setVisibility(View.VISIBLE);
                holder.name.setText(userInfo.getName());
                holder.photo.setImageResource(R.drawable.avatar_man);

                if (mIsDeleteModify){
                    holder.delete.setVisibility(View.VISIBLE);
                }else {
                    holder.delete.setVisibility(View.GONE);
                }
            }
            //点击事件的处理
            if(position == getCount() -1){  //减号
                    holder.photo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!mIsDeleteModify){
                                mIsDeleteModify = true;
                                notifyDataSetChanged();
                            }

                        }
                    });
            }else if (position == getCount() -2){ //加号
                holder.photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnGroupDetailListener.onAddMembers();
                    }
                });
            }else {
                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnGroupDetailListener.onDeleteMembers(userInfo);
                    }
                });
            }

        }else {
            //普通成员
            if (position == getCount() -1 ||position == getCount() -2 ){
                    convertView.setVisibility(View.GONE);
            }else {
                convertView.setVisibility(View.VISIBLE);

                holder.name.setText(userInfo.getName());
                holder.photo.setImageResource(R.drawable.avatar_man);
                //删除按钮
                holder.delete.setVisibility(View.GONE);
            }
        }
        //返回view

        return convertView;
    }
    private class  ViewHolder{
        private ImageView photo;
        private ImageView delete;
        private TextView name;

    }
    public interface OnGroupDetailListener{
        //添加群成员
        void onAddMembers();

        //删除群成员方法
        void onDeleteMembers(UserInfo user);
    }
}
